package com.itheima.management.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.management.vo.PageResult;
import com.itheima.management.vo.ManageVideos;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.Video;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class ManageVideoService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;

    @Autowired
    private RedisTemplate redisTemplate;



    /**
     *                  视频记录翻页
     *
     * @param userId            用户id
     * @param page
     * @param pageSize
     * @param sortProp          排序字段
     * @param sortOrder         ASC升序  DESC降序
     * @return
     */
    public PageResult queryVideoList(Long userId, Integer page, Integer pageSize, String sortProp, String sortOrder) {

        //定义PageResult对象,最终返回前端
        PageResult pageResult = new PageResult();
        //设置分页参数
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);

        PageInfo<Video>  pageInfo = quanZiApi.queryVideoListById(userId,page,pageSize,sortProp,sortOrder);
        //获取查询的列表数据
        List<Video> records = pageInfo.getRecords();
        //定义返回前台的items集合,将records中的数据放入items
        List<ManageVideos> videoVoList = new ArrayList<>();
        for (Video record : records) {
            ManageVideos videoVo = new ManageVideos();

            videoVo.setUserId(record.getUserId());
            videoVo.setPicUrl(record.getPicUrl());
            videoVo.setCreateDate(record.getCreated());
            videoVo.setVideoUrl(record.getVideoUrl());
            videoVo.setId(record.getId().toHexString());

            videoVo.setReportCount(RandomUtils.nextInt(0,10));
            videoVo.setLikeCount(RandomUtils.nextInt(20,100));  // TODO 点赞数量
            videoVo.setCommentCount(RandomUtils.nextInt(30,200));// TODO评论数
            videoVo.setForwardingCount(RandomUtils.nextInt(10,100));

            videoVoList.add(videoVo);
        }
        //装入总数
        pageResult.setCounts(pageInfo.getTotal());

        pageResult.setItems(videoVoList);

        return pageResult;
    }
}
